//turret.txt - Simple script for turrets
//Cell 0,1 - Stuff done flag. If both 0, nothing. Otherwise If non-zero, this 
//turret wont attack. If the turret is itself attacked, will set the not-attack setting back to 0.
//Cell 2,3 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

short i,target;
short mess1 = 0;
short mess2 = 0;
short mess3 = 0;
short last_abil;

body;

beginstate INIT_STATE;
	set_name(ME,"Presence-Mind");
	set_boss_level(ME,2);
	place_particle_num(ME,18,12,4);		
	place_particle_num(ME,19,12,4);		
	place_particle_num(ME,20,12,4);		
	set_walk_speed(ME,0);
	
	set_resistance(ME,0,98);
	set_resistance(ME,1,98);
	set_resistance(ME,2,98);
	set_resistance(ME,3,98);
	set_resistance(ME,4,98);
	set_resistance(ME,5,98);
	set_resistance(ME,6,98);
	set_resistance(ME,7,98);
	break;

beginstate DEAD_STATE;
	sf(5,2,1);
	sf(5,20,1);
	sf(2,3,2);
	award_party_xp(200,5);
	begin_talk_mode(77);
break;

beginstate START_STATE; 
	if (gf(7,28) < 2)
		end();
		
	if ((num_chars_in_group(3) == 0) && (mess1 == 0)) {
		mess1 = 1;
		last_abil = get_current_tick();

		activate_hidden_group(3);
		add_range_to_group(72,79,3);
		set_summon_level(1003,1);
		set_level(1003,1);
		
		begin_talk_mode(75);
		}
	if ((num_chars_in_group(3) == 0) && (mess1 > 0) && (mess2 == 0)) {
		mess2 = 1;
		last_abil = get_current_tick();

		activate_hidden_group(3);
		add_range_to_group(72,79,3);
		set_summon_level(1003,1);
		set_level(1003,1);

		clean_off_particles(ME);

		set_resistance(ME,0,0);
		set_resistance(ME,1,0);
		set_resistance(ME,2,0);
		set_resistance(ME,3,0);
		set_resistance(ME,4,0);
		set_resistance(ME,5,0);
		set_resistance(ME,6,0);
		set_resistance(ME,7,0);

		set_attitude(ME,10);
			
		begin_talk_mode(76);
		}
	if (mess2 == 0)
		end();

	if (who_shot_me() >= 0) {

		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

		
	if (get_foe_target(ME,8,0)) {

		do_attack();
		set_state(3);
		}
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);
	if (dist_to_char(get_target()) > 8) {
		set_foe_target(ME,-1);
		set_state(START_STATE);
		}

	if ((can_see_char(get_target())) && (is_combat()) && (tick_difference(last_abil,get_current_tick()) > 2)) {

		i = random_group_member(0);
		if (can_see_char(i) == FALSE)
			i = random_group_member(0);
		if (can_see_char(i) == FALSE)
			i = random_group_member(0);
		if (can_see_char(i)) {
			run_char_animation(2,1,35);	
			if (pc_num() == i) {
				print_named_str(ME,"magnifies your confusion! You feel sluggish.");
				if (get_char_status(i,1) >= 0)
					set_char_status(i,1,-8);
					else set_char_status(i,0,-8);
				shoot_projectile(ME,get_target(),38);
				pc_heard_sound_delay(128,100);						
				}
				else {
					print_named_str(ME,"terrifies one of your allies.");
					shoot_projectile(ME,get_target(),38);
					set_char_status(i,7,20);
					pc_heard_sound_delay(124,100);			
					}
			}
		
		last_abil = get_current_tick();
		end();
		}

	do_attack();

	turret_heal();
break;

beginstate TALKING_STATE;
break;